package com.project.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.project.music.bean.Singer;
import com.project.music.service.SingerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.project.music.utils.Consts;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 歌手控制类
 */

@Api(tags = "歌手控制类")
@RestController
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    private SingerService singerService;

    //添加歌手
    @ApiOperation("添加歌手")
    @PostMapping("/add")
    public JSONObject addSinger(Singer singer) {
        JSONObject jsonObject = new JSONObject();

        boolean status = singerService.save(singer);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "添加成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "添加失败");
        }
        return jsonObject;
    }

    //更新歌手
    @ApiOperation("更新歌手")
    @PostMapping("/update")
    public JSONObject updateSinger(Singer singer) {
        JSONObject jsonObject = new JSONObject();

        boolean status = singerService.updateById(singer);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }
        return jsonObject;
    }

    //删除歌手
    @ApiOperation("删除歌手")
    @GetMapping("/delete")
    public Boolean deleteSinger(@RequestParam("id") @ApiParam("歌手ID") int id) {

        Singer singer = singerService.getById(id);
        if (!singer.getPic().equals("/img/singerPic/hhh.jpg")) {
            String picPath = Consts.ROOT + singer.getPic();
            File picFile = new File(picPath);
            return singerService.removeById(id) && picFile.delete();
        } else {
            return singerService.removeById(id);
        }
    }

    //返回所有歌手
    @ApiOperation("返回所有歌手")
    @GetMapping("")
    public List<Singer> allSinger() {
        return singerService.list();
    }

    //根据姓名查找歌手
    @ApiOperation("根据姓名查找歌手")
    @GetMapping("/name/detail")
    public List<Singer> selectByName(@RequestParam("name") @ApiParam("歌手名") String name) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .like(Singer::getName, name);
        return singerService.list(queryWrapper);
    }

    //根据性别查找歌手
    @ApiOperation("根据性别查找歌手")
    @GetMapping("/sex/detail")
    public List<Singer> selectBySex(@RequestParam("sex") @ApiParam("性别") byte sex) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Singer::getSex, sex);
        return singerService.list(queryWrapper);
    }

    //更新歌手头像
    @ApiOperation("更新歌手头像")
    @PostMapping("/avatar/update")
    public JSONObject updateSingerAvator(@RequestParam("file") @ApiParam("头像文件") MultipartFile avatarFile,
                                     @RequestParam("id") @ApiParam("歌手ID") int id) {
        JSONObject jsonObject = new JSONObject();

        if (avatarFile.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "文件上传失败！");
            return jsonObject;
        }
        String fileName = System.currentTimeMillis() + avatarFile.getOriginalFilename();
        String filePath = Consts.ROOT + Consts.SEPARATOR + "img" + Consts.SEPARATOR + "singerPic";

        File path = new File(filePath);
        if (!path.exists()) {
            path.mkdir();
        }

        File dest = new File(filePath + Consts.SEPARATOR + fileName);
        String storeAvatarPath = "/img/singerPic/"+fileName;
        try {
            avatarFile.transferTo(dest);
            UpdateWrapper<Singer> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                         .eq(Singer::getId, id)
                         .set(Singer::getPic, storeAvatarPath);
            boolean status = singerService.update(updateWrapper);
            if (status) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "上传成功");
            }
            else {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "上传失败");
            }
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "上传失败" + e.getMessage());
            return jsonObject;
        }
    }
}